package com.chx.controller;

import com.chx.dto.SaleOrderDto;
import com.chx.dto.SaleOrderPageQueryDto;
import com.chx.dto.StockOutOrderDto;
import com.chx.result.PageResult;
import com.chx.result.Result;
import com.chx.service.SaleOrderService;
import com.chx.vo.SaleOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author cp-月入过万
 * @Date 2025/5/30
 * 销售订单控制器
 */
@RestController
@RequestMapping("/admin/sale-order")
@Api(tags = "销售订单相关接口")
@Slf4j
public class SaleOrderController {
    @Autowired
    private SaleOrderService saleOrderService;
    /**
     * 新增销售订单
     */
    @ApiOperation(value = "新增销售订单")
    @PostMapping
    public Result<String> save(@RequestBody SaleOrderDto saleOrderDto) {
        log.info("新增销售订单：{}", saleOrderDto);
        saleOrderService.save(saleOrderDto);
        return Result.success();
    }
    /**
     * 修改销售订单
     */
    @ApiOperation(value = "修改销售订单")
    @PutMapping
    public Result<String> update(@RequestBody SaleOrderDto saleOrderDto) {
        log.info("修改销售订单：{}", saleOrderDto);
        saleOrderService.update(saleOrderDto);
        return Result.success();
    }
    /**
     * 取消销售订单
     */
    @ApiOperation(value = "取消销售订单")
    @PutMapping("/status/cancel/{id}")
    public Result<String> cancel(@PathVariable Long id) {
        log.info("取消销售订单:{}",  id);
        saleOrderService.cancel(id);
        return Result.success();
    }
    /**
     * 根据id获取销售订单详情
     */
    @ApiOperation(value = "根据id获取销售订单详情")
    @GetMapping("/{id}")
    public Result<SaleOrderVo> getById(@PathVariable Long id) {
        log.info("根据id获取销售订单详情:{}",  id);
        SaleOrderVo saleOrderVo = saleOrderService.getById(id);
        return Result.success(saleOrderVo);
    }
    /**
     * 分页查询销售订单
     */
    @ApiOperation(value = "分页查询销售订单")
    @GetMapping("/page")
    public Result<PageResult> page(SaleOrderPageQueryDto saleOrderPageQueryDto) {
        log.info("分页查询销售订单：{}", saleOrderPageQueryDto);
        PageResult pageResult = saleOrderService.page(saleOrderPageQueryDto);
        return Result.success(pageResult);
    }
    /**
     * 审核订单
     */
    @ApiOperation(value = "审核订单")
    @PutMapping("/status/{id}")
    public Result<String> updateStatus(@PathVariable Long id) {
        log.info("审核订单:{}",  id);
        saleOrderService.updateStatus(id);
        return Result.success();
    }
    /**
     * 提交订单
     */
    @ApiOperation(value = "提交订单")
    @PostMapping("/status")
    public Result<String> commit(@RequestBody StockOutOrderDto stockOutOrderDto) {
        log.info("提交订单：{}", stockOutOrderDto);
        saleOrderService.commit(stockOutOrderDto);
        return Result.success();
    }
    /**
     * 订单发货
     */
    @ApiOperation(value = "订单发货")
    @PutMapping("/status/delivery/{id}")
    public Result<String> delivery(@PathVariable Long id) {
        log.info("订单发货：{}", id);
        saleOrderService.delivery(id);
        return Result.success();
    }
}
